package com.duoec.magichour.controller.api;

import com.duoec.magichour.core.annotation.Access;
import com.duoec.magichour.core.web.BaseApiController;
import com.duoec.magichour.dto.BaseResponse;
import com.duoec.magichour.dto.response.MessageDetailDto;
import com.duoec.magichour.dto.response.MessageListItemDto;
import com.duoec.magichour.service.MessageService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息接口
 */
@RestController
@RequestMapping("/api/message")
public class MessageApiController extends BaseApiController {
    @Autowired
    private MessageService messageService;

    /**
     * 获取消息列表
     *
     * @param messageType 消息类型，默认为未读；-1=已删除 0=未读 1=已读
     * @param pageNo      当前分页数，1为第一页
     * @param pageSize    每页最大记录数
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public BaseResponse<List<MessageListItemDto>> listMessage(
            @RequestParam(required = false, defaultValue = "0") int messageType,
            @RequestParam int pageNo,
            @RequestParam(required = false, defaultValue = "20") int pageSize
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        List<MessageListItemDto> messageList = messageService.listMessage(userId, messageType, pageNo, pageSize);
        return BaseResponse.success(messageList);
    }

    /**
     * 获取某条消息详细信息，供消息详情页展示
     *
     * @param id 消息ID
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.GET, value = "/{id:\\d+}")
    public BaseResponse<MessageDetailDto> get(
            @PathVariable long id
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        MessageDetailDto message = messageService.getDetail(id);
        if (message == null || message.getMessageStatus() == null || message.getMessageStatus().intValue() == -1) {
            //消息不存在或已被删除
            return BaseResponse.notFindError("消息不存在或已被删除！");
        }
        if (message.getReceiverId() == null || !message.getReceiverId().equals(userId)) {
            //无权限阅读此消息
            return BaseResponse.forbidError("你没有权限阅读此消息！");
        }
        return BaseResponse.success(message);
    }

    /**
     * 删除某个消息
     *
     * @param id 消息ID
     * @return 返回已删除的消息ID
     */
    @Access
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:\\d+}")
    public BaseResponse<Long> delete(
            @PathVariable long id
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        messageService.delete(userId, Lists.newArrayList(id));
        return BaseResponse.success(id);
    }

    /**
     * 将某个消息标识为已读
     *
     * @param id 消息ID
     * @return 返回已读的消息ID
     */
    @Access
    @RequestMapping(method = RequestMethod.POST, value = "/{id:\\d+}")
    public BaseResponse<Long> read(
            @PathVariable long id
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        messageService.read(userId, Lists.newArrayList(id));
        return BaseResponse.success(id);
    }
}
